package tree.test429;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/7/18 8:55
 */
public class Solution1 {

    class Node {
        public int val;
        public List<Node> children;

        public Node() {
        }

        public Node(int _val) {
            val = _val;
        }

        public Node(int _val, List<Node> _children) {
            val = _val;
            children = _children;
        }
    }

    // BFS
    public List<List<Integer>> levelOrder(Node root) {
        List<List<Integer>> res = new ArrayList<>();
        if (root == null) return res;
        Queue<Node> q = new LinkedList<>();
        q.add(root);
        Node temp;
        while (!q.isEmpty()) {
            int size = q.size();
            List<Integer> t = new ArrayList<>();
            for (int i = 0; i < size; i++) {
                temp = q.poll();
                t.add(temp.val);
                for (Node n : temp.children) {
                    q.add(n);
                }
            }
            res.add(t);
        }
        return res;
    }
}
